<?php
session_start();
set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
$PEAR_PATH = 'PEAR.php';
include ('Request/Request.php');
include "functions.php";
$lang = getLang();
$versionInt = getClientIntVersion();

    global $viewstate;
    global $postback;
    global $cookie;

    if (isset($_POST["latitude"]))
    $lat = $_POST["latitude"];
    else
    $lat = $_SESSION["latitude"];
    if (isset($_POST["longitude"]))
    $lon = $_POST["longitude"];
    else
    $lon = $_SESSION["longitude"];
    if (isset($_POST["finds"]))
    $finds = $_POST["finds"];
    else
    $finds = $_SESSION["finds"];
    
    //get page with nearest caches
    $url = "http://www.geocaching.com/seek/nearest.aspx?lat=".$lat."&lon=".$lon;
    if($finds!='0') $url .= "&f=".$finds;
    $req = & new HTTP_Request($url);
    $req->setMethod(HTTP_REQUEST_METHOD_GET);
    $req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
    $req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
    $req->sendRequest();
    $response = $req->getResponseBody();
    //echo '<pre>'.$response.'</pre>';


    $position = strpos($response, '__VIEWSTATE');
    #$viewstate = parseBetween('<table id="dlResults" cellspacing="0" cellpadding="4" border="0" border="0" style="width:100%;border-collapse:collapse;">', '</table>');
    $viewstate = parseBetween('<table id="ctl00_ContentBody_dlResults" class="SearchResultsTable" cellspacing="0" border="0" style="border-collapse:collapse;">', '</table>');
    $viewstate = trim(str_replace('"', '', $viewstate));

    //parse table with nearest caches
    $caches = parseTable($viewstate);
    $position=0;
    //get cache name, distance and link
    $parsedCaches = array();
    foreach($caches as $cache) {
        if(!strstr($cache[5], "<strike>")) {
            $position = 0;
            $response = $cache[2];
            $typeNumber = parseBetween('WptTypes/', '.gif');
            switch($typeNumber) {
                case '2': $type = 'TR'; break;
                    case '3': $type = 'MU'; break;
                        case '4': $type = 'VI'; break;
                            case '5': $type = 'LB'; break;
                                case '6': $type = 'EV'; break;
                                    case '8': $type = 'UN'; break;
                                        case '11': $type = 'WC'; break;
                                            case '12': $type = 'LL'; break;
                                                case '13': $type = 'CI'; break;
                                                    case 'earthcache': $type = 'EC'; break;
                                                        case '1304': $type = 'AME'; break;
                                                            case '1858': $type = 'WH'; break;
                                                                case 'mega': $type = 'ME'; break;
                                                                    case 'ape_32': $type = 'APE'; break;
                                                                        default: $type = '';
                                                                        }
                                                                        $a = strpos($cache[0], 'compass/')+8;
                                                                        $b = strpos($cache[0], '.gif');
                                                                        $compas = trim(substr($cache[0], $a, $b-$a));
                                                                        if (strLen($compas) > 2) {
                                                                        	$compas = '';
                                                                        } else {
                                                                        	$compas = $lang[$compas];
                                                                        	$compas .= ' - ';
                                                                        }
                                                                        $a = strpos($cache[0], '<br />')+6;
                                                                        $b = strlen($cache[0]);
                                                                        $distance = trim(substr($cache[0], ($a-$b)));
                                                                        $response = $cache[5];
                                                                        $link = parseBetween('<a href=', '>');
                                                                        $response = $cache[5];
                                                                        $name = removeHTML(parseBetween('>', '</a>'), true);
                                                                        $link = explode('?', $link);
                                                                        $params = explode('&', $link[1]);
                                                                        $params_arr = array();
                                                                        foreach($params as $param) {
                                                                            $foo = explode('=', $param);
                                                                            $params_arr[$foo[0]] = $foo[1];
                                                                        }
                                                                        if($versionInt && $versionInt < 91) {
                                                                            $parsedCaches[] = '<li><a href="'.SERVICE_URL.'cache.php?guid='.$params_arr['guid'].'" class="listItem">['.$type.'] '.$distance.' '.$compas.$name.'</a></li>'."\n";
                                                                        }
                                                                        else
                                                                        {
                                                                            $parsedCaches[] = '<li><img src="'.SERVICE_URL.'icons/'.$type.'.png" /><a href="'.SERVICE_URL.'cache.php?guid='.$params_arr['guid'].'" class="listItem">'.$distance.' '.$compas.$name.'</a></li>'."\n";
                                                                        }
                                                                    }
                                                                }
                                                                if(count($parsedCaches)==0) {	//output if no caches found
                                                                    $html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>Warning</title>
              </head>
              <body class="alert">
            <p class="text">'.$lang['no_caches'].'.</p>
            <a href="'.SERVICE_URL.'first.php" class="next">'.$lang['new_search'].'</a>
        <locify:timeout value="6000" />';
                                                                } else {	//output list of caches
                                                                    $html = '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>'.$lang['nearest_caches'].'</title>
              </head>
              <body class="list"><ul>'."\n";
                                                                    foreach($parsedCaches as $parsedCache) $html .= $parsedCache;
                                                                    $html .= "</ul>\n";
                                                                }
                                                                $html .= "</body>\n</html>";

                                                                echo $html;
                                                            ?>
